package com.hca.training.zk.javabasic.Socket;

import java.net.*;
import java.util.Enumeration;

public class InetAddressExample {
    public static void  main(String[] arg0) throws SocketException {
        //获取主机的网路接口列表
        Enumeration<NetworkInterface> interfaceEnumeration=NetworkInterface.getNetworkInterfaces();
        if (interfaceEnumeration==null)
        {
            System.out.println("--   并没有  --");
        }else {
            //获取并打印出列表中每个接口的地址
            while(interfaceEnumeration.hasMoreElements())
            {
                NetworkInterface networkInterface=interfaceEnumeration.nextElement();
                System.out.println("接口-"+networkInterface.getName()+":");
                Enumeration<InetAddress> addressList=networkInterface.getInetAddresses();
                if (!addressList.hasMoreElements())
                {
                    System.out.println("--  接口没有地址  --");
                }
                while(addressList.hasMoreElements())
                {
                    InetAddress address=addressList.nextElement();
                    System.out.println("\tAddress"+(address instanceof Inet4Address ? "(V4)" : (address instanceof Inet6Address ? "(V6)" : "(?)")));
                    System.out.println(":"+address.getHostAddress());
                }
            }
        }
        for(String host :arg0)
        {
            System.out.println(host+":");
            try {
                InetAddress[] addressList=InetAddress.getAllByName(host);
                for(InetAddress address :addressList)
                {
                    System.out.println("\t"+address.getHostName()+"/"+address.getHostAddress());
                }
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
        }


    }
}
